﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.Entity.AREntity;
using Wicresoft.ARConnector;
using Wicresoft.Service.Tasks;
using System.Configuration;
using System.IO;
using System.Net;
using Wicresoft.Logger;

namespace Wicresoft.Task.SCCMSync
{
    public class SCCMSyncTask : IntervalTask
    {
        private static readonly string ARServer = ConfigurationManager.AppSettings["ARServer"];
        private static readonly string ARUid = ConfigurationManager.AppSettings["ARUid"];
        private static readonly string ARPwd = ConfigurationManager.AppSettings["ARPwd"];
        private static readonly string CSVUrl = ConfigurationManager.AppSettings["CSVUrl"];
        private static readonly string AD4 = ConfigurationManager.AppSettings["AD4"];
        private static readonly string Domain = ConfigurationManager.AppSettings["Domain"];
        private static readonly string Password = ConfigurationManager.AppSettings["Password"];

        protected override void Execute()
        {
            Log.Info(this.Name,"SCCM task has beging...");
            try
            {
                WebClient client = new WebClient();
                client.Credentials = new NetworkCredential(AD4, Password, Domain);
                // Downlod csv data from the url they given
                byte[] webContent = client.DownloadData(CSVUrl);
                string html = Encoding.Default.GetString(webContent);
                Stream stream = new MemoryStream(webContent);

                StreamReader sr = new StreamReader(stream);
                string[] titleArray = sr.ReadLine().Replace("\"", "").Split(',');
                titleArray[0] = "ID";
                List<Server> serverList = new List<Server>();
                // Initial computer system object

                // Add data to AR server
                using (ARProxy<Server> proxy = new ARProxy<Server>(ARUid, ARPwd, ARServer, string.Empty))
                {
                    while (!sr.EndOfStream)
                    {
                        string[] content = sr.ReadLine().Replace("\"", "").Split(',');
                        Server server = new Server(content, titleArray);
                        server.OwnerName = "";
                        server.Submitter = ARUid;
                        server.IsVirtual = false;
                        server.Status = 0;
                        server.Description = DateTime.Now.ToString();
                        server.DatasetId = "BMC.ASSET";
                        server.Name = server.SCCMId;
                        server.Item = "Server";
                        // serverList.Add(server);
                        proxy.CreateEntry(server);
                    }
                    // proxy.CreateEntry(serverList.Take(10).ToList());
                }
     
            }
            catch (Exception ex)
            {
                Log.Exception(this.Name, ex);

            }
            Log.Info(this.Name, "SCCM task has completed!");
        }
    }
}
